package de.uni.bremen.estudent.lbs.models;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;

import de.uni.bremen.estudent.lbs.ui.FoodDialog;
import de.uni.bremen.estudent.lbs.ui.InformationDialog;
import de.uni.bremen.estudent.lbs.util.Constants;

/**
 * @author bahlfeld
 * @version 1.0 24.01.2012
 */
public final class MyItemizedOverlay extends ItemizedOverlay<LbsOverlayItem> {

    /**
     * context.
     */
    private Context mContext;
    /**
     * The old zoom level of the mapview.
     */
    private int mOldZoomLevel = 0;

    /**
     * List of all overlay items.
     */
    private final ArrayList<LbsOverlayItem> mOverlays = new ArrayList<LbsOverlayItem>();

    /**
     * Constructor of the class.
     *
     * @param defaultMarker
     *            defaut marker for the Overlays.
     */
    public MyItemizedOverlay(final Drawable defaultMarker) {
        super(boundCenter(defaultMarker));
        this.populate();
    }

    /**
     * Constructor of the class.
     *
     * @param defaultMarker
     *            default marker for the overlays.
     * @param context
     *            The context object to handle on tap events.
     */
    public MyItemizedOverlay(final Drawable defaultMarker, final Context context) {
        super(defaultMarker);
        this.mContext = context;
        this.populate();
    }

    /**
     * Adds an overlayitem to the array list of overlayitems and populate it.
     *
     * @param overlayItem
     *            An overlayitem which should be add to the list.
     */
    public void addOverlay(final LbsOverlayItem overlayItem) {
        this.mOverlays.add(overlayItem);
        this.populate();
    }

    @Override
    public void draw(final Canvas canvas, final MapView mapView,
            final boolean shadow) {
        final int newZoomLevel = mapView.getZoomLevel();

        if (newZoomLevel != this.mOldZoomLevel) {
            this.mOldZoomLevel = newZoomLevel;
            this.scaleMarkers(newZoomLevel);
        }
        super.draw(canvas, mapView, false);

    }

    @Override
    public boolean onTap(final int index) {
        LbsOverlayItem item = this.mOverlays.get(index);


         if (Constants.GASTRONOMIE.equals(item.getCategory())) {
            // Start FoodDialog...
            new FoodDialog(this.mContext, item);
        } else {
            // Start InformationDialog...
            new InformationDialog(this.mContext, item);

        }
        return true;

    }

    /**
     * Removes an overlay from the list.
     *
     * @param index
     *            index of the overlay.
     */
    public void removeItem(final int index) {
        this.mOverlays.remove(index);
        this.populate();
    }

    @Override
    public int size() {
        return this.mOverlays.size();
    }

    @Override
    protected LbsOverlayItem createItem(final int pos) {
        return this.mOverlays.get(pos);
    }

    /**
     * Scales a marker when the zoom level was changed.
     *
     * @param zoomLevel
     *            zoom level value.
     */
    private void scaleMarkers(int zoomLevel) {
        if (this.mOverlays == null) {
            return;
        }

        int iconSize = this.calculateIconSize(zoomLevel);

        // scale every marker
        for (LbsOverlayItem item : this.mOverlays) {
            Drawable drawIcon = item.getMarker(0);
            if (drawIcon != null) {
                drawIcon.setBounds(0, 0, iconSize, iconSize);
            }
        }
    }

    /**
     * Calculates the right icon size by the given zoom level.
     *
     * @param zoomLevel
     *            zoom level value
     * @return new icon size
     */
    private int calculateIconSize(int zoomLevel) {
        int iconSize;
        switch (zoomLevel) {
        case Constants.ZOOM_LEVEL_15:
            iconSize = Constants.ICON_SIZE_20;
            break;
        case Constants.ZOOM_LEVEL_16:
            iconSize = Constants.ICON_SIZE_30;
            break;
        case Constants.ZOOM_LEVEL_17:
            iconSize = Constants.ICON_SIZE_40;
            break;
        case Constants.ZOOM_LEVEL_18:
            iconSize = Constants.ICON_SIZE_50;
            break;
        case Constants.ZOOM_LEVEL_19:
            iconSize = Constants.ICON_SIZE_60;
            break;
        case Constants.ZOOM_LEVEL_20:
            iconSize = Constants.ICON_SIZE_70;
            break;
        case Constants.ZOOM_LEVEL_21:
            // greatest zoom level
            iconSize = Constants.ICON_SIZE_80;
            break;
        default:
            // default case is a low zoom level
            iconSize = Constants.ICON_SIZE_10;
            break;
        }
        return iconSize;
    }
}
